﻿(function ($) {

    var dataTable;
    var datalistId;
    var dataListPage=new Pager();
    $.fn.DataList = function (options) {

        var defaultOptions ={
            type:'post',
            url: null,
            height: null,
            width: null,
            dataType: 'Json',
            data: null,
            pagesize: 20,
            template: null,
            checkAll:null
            }
        var opts = $.extend(defaultOptions, options);


        function InitialDataSource(element)
        {

            $('tbody', element).text('');
            
            var $tbody = $('tbody', element);
            $.ajax({

                type: options.type,
                url: opts.url,
                datatype: opts.datatype,
                data: opts.data + "&page=" + dataListPage.GetCurrentPage()+"&pagesize="+opts.pagesize,
                beforeSend: function () {

                    $('.small_wait', $('#' + datalistId)).show();

                    
                    
                },
                success: function (data) {

                   
                    $('tr', $tbody).remove();
                    if (data.DataSource)
                    {

                        $.tmpl(opts.template, data.DataSource).appendTo($tbody);
                        dataTable = new DataTable(data.DataSource);

                        InitialPager(element, data.RowCount, dataListPage.GetCurrentPage);
                    }

                },
                error: function (e)
                {
                    alert(e.responseText);
                },

                complete: function ()
                {
                    $('.small_wait', $('#' + datalistId)).hide();

                }
            });


        }

        function InitialPager(element,rowCount,currentPage)
        {
            

            if (!currentPage) {
                dataListPage = new Pager(rowCount, pagesize);
            }
            else
            {
                var currentPage = dataListPage.GetCurrentPage();
              
                dataListPage = new Pager(rowCount, opts.pagesize, currentPage);
            }

            $('.small_pager', $('#' + datalistId)).remove();
            var $pagerContainer = $("<div class=\"small_pager\"></div>");

            var $firstPage = $("<span class=\"firstPage\"><input type=\"hidden\" value=\"1\" />首页</span>");
            var $proviewPage = $("<span class=\"proviewPage\"><input type=\"hidden\" value=\"" + dataListPage.GetProviewPage() + "\" />上一页</span>");
            var $nextPage = $("<span class=\"nextPage\"><input type=\"hidden\" value=\"" + dataListPage.GetNextPage() + "\"  />下一页</span>");
            var $maxPage = $("<span class=\"maxPage\"><input type=\"hidden\" value=\"" + dataListPage.GetMaxPage() + "\"  />末页</span>");
            var $totalPage = $("<span class=\"totalPage\"></span>");
            var $selectPage = $("<select></select>");

            var pageListItem = 0;
           
      
            if (dataListPage.GetCurrentPage() < dataListPage.GetMaxPage()) {

                
             
                for (var i = ( parseInt(dataListPage.GetCurrentPage())-5); i <dataListPage.GetCurrentPage(); i++) {

                    if (i >= 1) {  
                    $selectPage.append("<option value=\"" + i + "\" >" + i + "</option>");
                    pageListItem++;
                }
                }
                var index = 0;
                for (var i = dataListPage.GetCurrentPage() ; i <= dataListPage.GetMaxPage() ; i++) {

                    if (pageListItem == 10) {
                        break;
                    }
                    if (index==0) {
                        $selectPage.append("<option value=\"" + i + "\" selected=\"selected\">" + i + "</option>");
                    } else {
                        $selectPage.append("<option value=\"" + i + "\">" + i + "</option>");
                    } pageListItem++;
                    index = 1;

                }
            } else
            {
               
                for (var i = dataListPage.GetMaxPage() ; i >= 1 ; i--) {

                    if (pageListItem == 10) {
                       break;
                    }
                    $selectPage.append("<option value=\"" + i + "\">" + i + "</option>");
                    pageListItem++;

                }

            }

            $totalPage.append($selectPage);
            PageItemClick($firstPage, element);
            PageItemClick($proviewPage, element);
            PageItemClick($nextPage, element);
            PageItemClick($maxPage, element);
            PageItemSelectChange($selectPage, element);

           
           
            $pagerContainer.append($firstPage);
            $pagerContainer.append($proviewPage);
            $pagerContainer.append($nextPage);
            $pagerContainer.append($maxPage);
            $pagerContainer.append($totalPage);
            $('#' + datalistId).append($pagerContainer);
        }

        function PageItemClick(pageItem, element)
        {
            pageItem.click( function ()
            {
                     
                var currentPage=$("input[type='hidden']",pageItem).val();
                if (currentPage) {
                    if (parseInt(currentPage) == 1 && dataListPage.GetCurrentPage()==1)
                    {
                        return;
                    }
                    if (parseInt(currentPage) == dataListPage.GetMaxPage() && parseInt(currentPage)==dataListPage.GetCurrentPage())
                    {
                        return;
                    }
                      dataListPage.SetCurrentPage(currentPage);
                      InitialDataSource(element);
                   
                  
                }
            })
        }

        function PageItemSelectChange(pageItem, element)
        {
            

            pageItem.change(function ()
            {
               
                var currentPage = $('option:selected', pageItem).val();
                if (currentPage) {
                    dataListPage.SetCurrentPage(currentPage)
                    InitialDataSource(element);

                }

            })

        }
      
        //初始化页面布局
        function InitialDataListLayout(element)
        {
            
            var id = element.attr('id');
             datalistId=id= id + "smallDataList" ;
             element.wrap("<div id=\"" + datalistId + "\" class=\"smallDataList\"></div>").wrap("<div style=\"overflow:auto; height:90%;\"></div>");

             var $waitMessage = $("<div class=\"small_wait\"  ></div>");
            
            //加载等待提示框
            $('#' + datalistId).append($waitMessage);
            var dataListLeft = $('#' + datalistId).offset().left;
            var dataListTop = $('#' + datalistId).offset().top;
            var dataListWidth = $('#' + datalistId).width();
            var dataListHeight = $('#' + datalistId).height();
            var waitMessageWidth = $waitMessage.width();
            var waitMessageHeight = $waitMessage.height();
            var waitMessageLeft = (parseInt(dataListLeft) + parseInt(dataListWidth) - parseInt(waitMessageWidth / 2)) / 2;
            var waitMessageTop = (parseInt(dataListTop) + parseInt(dataListHeight) - parseInt(waitMessageHeight / 2)) / 2;
            $waitMessage.css({ 'left': waitMessageLeft + 'px', 'top': waitMessageTop + 'px' });

            if (!opts.height)
            {

                opts.height = "100%";
            }
            if (!opts.width)
            {
                opts.width = "100%";
            }
            $('#' + datalistId).css({ 'height': opts.height, 'width': opts.width });
            var $tbody = $("<tbody></tbody>");

            element.append($tbody);
        }


        InitialDataListLayout($(this));
        InitialDataSource($(this));
    }
    //刷新数据
    $.fn.Refrece = function (currentPage)
    {

    }

    //获取到指定行数据
    $.fn.GetRow=function(index)
    {


    }
    //获取到选中行的数据
    $.fn.GetSelectedRow=function()
    {

    }
    //获取到选中多行的数据
    $.fn.GetSelectedRows=function(index)
    {

    }
   //获取到指定列的数据
    $.fn.GetColumn=function(index)
    {

    }
    //删除数据
    $.fn.Delete=function(url,PrimaryKeyColumnName)
    {

    }
    //打开新窗口编辑
    $.fn.Edit=function(url,PrimaryKeyColumnName)
    {

    }
    //打开新窗口添加
    $.fn.Add=function(url)
    {


    }
})(jQuery)